package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * @author KK
 *
 */
public class StheadlineAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.stheadline.com/.*/[0-9]*/[0-9]*/[0-9]*[a-z][0-9]*.html.*",
		"http://.*.stheadline.com/dailynews/.*/[0-9]*/[0-9]*/[0-9]*/[0-9]*.asp",
		"http://.*.stheadline.com/dailynews/headline_news_detail_columnist.asp\\?id=[0-9]*&kw=[0-9]*&section_name=.*",
		"http://.*.stheadline.com/content.php\\?cat=.*&vid=[0-9]*",
		"http://.*.stheadline.com/inews-content.php\\?cat=.*&nid=[0-9]*",
		"http://.*.stheadline.com/oversea/[0-9]{4,}ao[0-9]*.html",
		"http://.*.stheadline.com/china/news_detail.asp\\?sid=[0-9]*",
		"http://.*.stheadline.com/breakingnews/[0-9]*[a-zA-Z]+[0-9]*.asp",
		"http://.*.stheadline.com/[a-z]*-content.php\\?cat=.*&nid=[0-9]*",
		"http://.*.stheadline.com/[a-z]*/[a-z]*_content.asp\\?contid=[0-9]*&srctype=.*"

	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		if (date==null)date=DateUtils.matchDate(doc.select(".newsheadline").text());
		if (date==null)date=DateUtils.matchDate(doc.select(".newsheadlinetime").text());
		if (date==null)date=DateUtils.matchDate(doc.select(".inews-content-datetime").text());
		if (date==null)date=DateUtils.matchDate(doc.select("#contentNewsDate").text());
		if (date==null)date=DateUtils.matchDate(doc.select(".time").text());
		if (date==null)date=DateUtils.matchDate(doc.select(".heading").text());


		if(StringUtils.isBlank(title)){
			title =doc.select(".inews-heading h1").text();
		}
		if(StringUtils.isBlank(title)){
			title =doc.select(".heading h1").text();
		}
		if(StringUtils.isBlank(title)){
			title = doc.select("title").text();
			title = StringUtils.substringBefore(title, "-");
		}

		if(StringUtils.isBlank(title)) title =doc.select(".headlinetitle").text();
		if(StringUtils.isBlank(title)) title =doc.select("#contentNewsTitle").text();
		if(StringUtils.isBlank(title)) title =doc.select(".inews-content-title").text();
		if(StringUtils.isBlank(title)) title =doc.select(".bodyhead").text();
		if(StringUtils.isBlank(title)) title =doc.select(".bodyhead_for_over_sea_content").text();
		if(title!=null) title=title.replace("相關新聞", "");

		doc.select(".bodyhead form").remove();
		doc.select(".newsheadline").remove();
		doc.select(".newsheadlinetime").remove();
		if(StringUtils.isBlank(content)) content=HtmlCleaner.getContentHtml(url,doc.select(".bodytext"));
		if(StringUtils.isBlank(content)) content=HtmlCleaner.getContentHtml(url,doc.select(".bodytext_v1"));
		if(StringUtils.isBlank(content)) content=HtmlCleaner.getContentHtml(url,doc.select(".newsContent"));
		if(StringUtils.isBlank(content)) content=HtmlCleaner.getContentHtml(url,doc.select("#carousel-inews-article,div.inews-content"));

		author=JSoupUtils.matchAuthor(doc, "来源：");
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
